home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
minix
/
up1510b.tgz
/
up1510b
/
src
/
commands
/
patch
/
pch.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-19
|
29KB
|
1,124 lines
/* $Header: pch.c,v 2.0.1.7 88/06/03 15:13:28 lwall Locked $
*
* $Log: pch.c,v $
* Revision 2.0.1.7 88/06/03 15:13:28 lwall
* patch10: Can now find patches in shar scripts.
* patch10: Hunks that swapped and then swapped back could core dump.
*
* Revision 2.0.1.6 87/06/04 16:18:13 lwall
* pch_swap didn't swap p_bfake and p_efake.
*
* Revision 2.0.1.5 87/01/30 22:47:42 lwall
* Improved responses to mangled patches.
*
* Revision 2.0.1.4 87/01/05 16:59:53 lwall
* New-style context diffs caused double call to free().
*
* Revision 2.0.1.3 86/11/14 10:08:33 lwall
* Fixed problem where a long pattern wouldn't grow the hunk.
* Also restored p_input_line when backtracking so error messages are right.
*
* Revision 2.0.1.2 86/11/03 17:49:52 lwall
* New-style delete triggers spurious assertion error.
*
* Revision 2.0.1.1 86/10/29 15:52:08 lwall
* Could falsely report new-style context diff.
*
* Revision 2.0 86/09/17 15:39:37 lwall
* Baseline for netwide release.
*
*/
#include "EXTERN.h"
#include "common.h"
#include "util.h"
#include "INTERN.h"
#include "pch.h"
/* Patch (diff listing) abstract type. */
static long p_filesize; /* size of the patch file */
static LINENUM p_first; /* 1st line number */
static LINENUM p_newfirst; /* 1st line number of replacement */
static LINENUM p_ptrn_lines; /* # lines in pattern */
static LINENUM p_repl_lines; /* # lines in replacement text */
static LINENUM p_end = -1; /* last line in hunk */
static LINENUM p_max; /* max allowed value of p_end */
static LINENUM p_context = 3; /* # of context lines */
static LINENUM p_input_line = 0; /* current line # from patch file */
static char **p_line = Null(char**); /* the text of the hunk */
static short *p_len = Null(short*); /* length of each line */
static char *p_char = Nullch; /* +, -, and ! */
static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */
static int p_indent; /* indent to patch */
static LINENUM p_base; /* where to intuit this time */
static LINENUM p_bline; /* line # of p_base */
static LINENUM p_start; /* where intuit found a patch */
static LINENUM p_sline; /* and the line number for it */
static LINENUM p_hunk_beg; /* line number of current hunk */
static LINENUM p_efake = -1; /* end of faked up lines--don't free */
static LINENUM p_bfake = -1; /* beg of faked up lines */
/* Prepare to look for the next patch in the patch file. */
void
re_patch()
{
p_first = Nulline;
p_newfirst = Nulline;
p_ptrn_lines = Nulline;
p_repl_lines = Nulline;
p_end = (LINENUM)-1;
p_max = Nulline;
p_indent = 0;
}
/* Open the patch file at the beginning of time. */
void
open_patch_file(filename)
char *filename;
{
if (filename == Nullch || !*filename || strEQ(filename, "-")) {
pfp = fopen(TMPPATNAME, "w");
if (pfp == Nullfp)
fatal2("patch: can't create %s.\n", TMPPATNAME);
while (fgets(buf, sizeof buf, stdin) != Nullch)
fputs(buf, pfp);
Fclose(pfp);
filename = TMPPATNAME;
}
pfp = fopen(filename, "r");
if (pfp == Nullfp)
fatal2("patch file %s not found\n", filename);
Fstat(fileno(pfp), &filestat);
p_filesize = filestat.st_size;
next_intuit_at(0L,1L); /* start at the beginning */
set_hunkmax();
}
/* Make sure our dynamically realloced tables are malloced to begin with. */
void
set_hunkmax()
{
#ifndef lint
if (p_line == Null(char**))
p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
if (p_len == Null(short*))
p_len = (short*) malloc((MEM)hunkmax * sizeof(short));
#endif
if (p_char == Nullch)
p_char = (char*) malloc((MEM)hunkmax * sizeof(char));
}
/* Enlarge the arrays containing the current hunk of patch. */
void
grow_hunkmax()
{
hunkmax *= 2;
/*
* Note that on most systems, only the p_line array ever gets fresh memory
* since p_len can move into p_line's old space, and p_char can move into
* p_len's old space. Not on PDP-11's however. But it doesn't matter.
*/
assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
#ifndef lint
p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
p_len = (short*) realloc((char*)p_len, (MEM)hunkmax * sizeof(short));
p_char = (char*) realloc((char*)p_char, (MEM)hunkmax * sizeof(char));
#endif
if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
return;
if (!using_plan_a)
fatal1("patch: out of memory (grow_hunkmax)\n");
out_of_mem = TRUE; /* whatever is null will be allocated again */
/* from within plan_a(), of all places */
}
/* True if the remainder of the patch file contains a diff of some sort. */
bool
there_is_another_patch()
{
if (p_base != 0L && p_base >= p_filesize) {
if (verbose)
say1("done\n");
return FALSE;
}
if (verbose)
say1("Hmm...");
diff_type = intuit_diff_type();
if (!diff_type) {
if (p_base != 0L) {
if (verbose)
say1(" Ignoring the trailing garbage.\ndone\n");
}
else
say1(" I can't seem to find a patch in there anywhere.\n");
return FALSE;
}
if (verbose)
say3(" %sooks like %s to me...\n",
(p_base == 0L ? "L" : "The next patch l"),
diff_type == CONTEXT_DIFF ? "a context diff" :
diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
diff_type == NORMAL_DIFF ? "a normal diff" :
"an ed script" );
if (p_indent && verbose)
say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
skip_to(p_start,p_sline);
while (filearg[0] == Nullch) {
if (force) {
say1("No file to patch. Skipping...\n");
filearg[0] = savestr(bestguess);
return TRUE;
}
ask1("File to patch: ");
if (*buf != '\n') {
if (bestguess)
free(bestguess);
bestguess = savestr(buf);
filearg[0] = fetchname(buf, 0, FALSE);
}
if (filearg[0] == Nullch) {
ask1("No file found--skip this patch? [n] ");
if (*buf != 'y') {
continue;
}
if (verbose)
say1("Skipping patch...\n");
filearg[0] = fetchname(bestguess, 0, TRUE);
skip_rest_of_patch = TRUE;
return TRUE;
}
}
return TRUE;
}
/* Determine what kind of diff is in the remaining part of the patch file. */
int
intuit_diff_type()
{
Reg4 long this_line = 0;
Reg5 long previous_line;
Reg6 long first_command_line = -1;
long fcl_line;
Reg7 bool last_line_was_command = FALSE;
Reg8 bool this_is_a_command = FALSE;
Reg9 bool stars_last_line = FALSE;
Reg10 bool stars_this_line = FALSE;
Reg3 int indent;
Reg1 char *s;
Reg2 char *t;
char *indtmp = Nullch;
char *oldtmp = Nullch;
char *newtmp = Nullch;
char *indname = Nullch;
char *oldname = Nullch;
char *newname = Nullch;
Reg11 int retval;
bool no_filearg = (filearg[0] == Nullch);
ok_to_create_file = FALSE;
Fseek(pfp, p_base, 0);
p_input_line = p_bline - 1;
for (;;) {
previous_line = this_line;
last_line_was_command = this_is_a_command;
stars_last_line = stars_this_line;
this_line = ftell(pfp);
indent = 0;
p_input_line++;
if (fgets(buf, sizeof buf, pfp) == Nullch) {
if (first_command_line >= 0L) {
/* nothing but deletes!? */
p_start = first_command_line;
p_sline = fcl_line;
retval = ED_DIFF;
goto scan_exit;
}
else {
p_start = this_line;
p_sline = p_input_line;
retval = 0;
goto scan_exit;
}
}
for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
if (*s == '\t')
indent += 8 - (indent % 8);
else
indent++;
}
for (t=s; isdigit(*t) || *t == ','; t++) ;
this_is_a_command = (isdigit(*s) &&
(*t == 'd' || *t == 'c' || *t == 'a') );
if (first_command_line < 0L && this_is_a_command) {
first_command_line = this_line;
fcl_line = p_input_line;
p_indent = indent; /* assume this for now */
}
if (!stars_last_line && strnEQ(s, "*** ", 4))
oldtmp = savestr(s+4);
else if (strnEQ(s, "--- ", 4))
newtmp = savestr(s+4);
else if (strnEQ(s, "Index:", 6))
indtmp = savestr(s+6);
else if (strnEQ(s, "Prereq:", 7)) {
for (t=s+7; isspace(*t); t++) ;
revision = savestr(t);
for (t=revisi